package com.wan37.logic.email.handler;

import com.wan37.logic.backpack.weaponry.WeaponryManager;
import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.Result;
import com.wan37.logic.domain.entity.UserDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.UserVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.email.domain.vo.AttachmentVO;
import com.wan37.logic.email.domain.vo.EmailVO;
import com.wan37.logic.email.serivce.EmailService;
import com.wan37.logic.email.util.EmailRoutes;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.UserService;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.service.cache.WeaponryServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.constant.CommonEmail;
import com.wan37.protobuf.constant.ShopLabel;
import com.wan37.protobuf.message.client2server.game.GameEmail;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-28 15:10
 **/
public class CGameWorldPlayerEmailHandler extends MsgHandler {
    private AsynDBManager asynDBManager;
    private EmailService emailService;
    private EmailRoutes emailRoutes;
    private UserServiceCache userServiceCache;
    private WeaponryManager weaponryManager;
    private WeaponryServiceCache weaponryServiceCache;
    private RouteUtils routeUtils;
    private UserService userService;
    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameWorldPlayerEmailHandler(MessageVO messageVO) {
        super(messageVO);
        this.asynDBManager = SpringContextHolder.getBean("asynDBManager");
        this.emailService = SpringContextHolder.getBean("emailService");
        this.emailRoutes = SpringContextHolder.getBean("emailRoutes");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.weaponryManager = SpringContextHolder.getBean("weaponryManager");
        this.weaponryServiceCache = SpringContextHolder.getBean("weaponryServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.userService = SpringContextHolder.getBean("userService");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameEmail.WorldPlayerEmailMsg worldPlayerEmailMsg = (GameEmail.WorldPlayerEmailMsg) messageVO.getMessage();
        int code = worldPlayerEmailMsg.getCode();
        switch (code){
            case CommonEmail.GET_ALL:
                getAllEmail(messageVO.getUserId());
                break;
            case CommonEmail.CLAIM:
                for (GameEmail.StringMsg stringMsg : worldPlayerEmailMsg.getEmailIdList()){
                    String emailId = stringMsg.getStringId();
                    List<EmailVO> emailVOList = emailService.findEmailVOByUserId(messageVO.getUserId());
                    for (EmailVO emailVO : emailVOList){
                        if (emailVO.getEmailId().equals(emailId)){
                            if (emailVO.getIsRead() == 0){
                                emailRoutes.sendEmailResponse(CommonEmail.CLAIM_ERROR, null, messageVO.getUserId());
                                return;
                            }
                            for (AttachmentVO attachmentVO : emailVO.getAttachmentVOList()){
                                if (attachmentVO.getType() == BackpackLabel.WEAPONRY){
                                    WeaponryVO weaponryVO = weaponryServiceCache.getWeaponryServiceMap().get(attachmentVO.getBackpackId()).getWeaponryVO();
                                    List<WeaponryVO> weaponryVOList = new ArrayList<>();
                                    long i = attachmentVO.getNumber();
                                    while (i > 0){
                                        i--;
                                        WeaponryVO newWeaponryVO = new WeaponryVO(weaponryVO);
                                        weaponryVOList.add(newWeaponryVO);
                                    }
                                    Result result = weaponryManager.emailAddWeaponry(messageVO.getUserId(), weaponryVOList);
                                    if (result.getCode() == 1){
                                        emailVO.setIsRead(0);
                                        asynDBManager.distributeWork(messageVO.getUserId(), ()->emailService.update(emailVO));
                                        routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS,
                                                "邮件领取成功！", userServiceCache.getUserServiceMap()
                                                        .get(messageVO.getUserId()).getWeaponryVOMap(), messageVO);
                                    }
                                    if (result.getCode() == 0){
                                        routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_ERROR,
                                                result.getContent(), null, messageVO);
                                    }
                                }else if (attachmentVO.getType() == ShopLabel.MONEY_TYPE_INGOT){
                                    emailVO.setIsRead(0);
                                    UserVO userVO = userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getUserVO();
                                    userVO.setIngot(userVO.getIngot() + attachmentVO.getNumber());
                                    asynDBManager.distributeWork(messageVO.getUserId(), ()->emailService.update(emailVO));
                                    asynDBManager.distributeWork(messageVO.getUserId(), ()->userService.update(new UserDO(userVO)));
                                    routeUtils.sendUserVO(userVO);
                                }
                            }
                        }
                    }

                }
                break;
            default:
                break;
        }
    }
    public void getAllEmail(String userId){
        List<EmailVO> emailVOList =  emailService.findEmailVOByUserId(userId);
        emailRoutes.sendEmailResponse(CommonEmail.GET_ALL, emailVOList, userId);
    }
}
